package com.bl.weilan.service.biz.train.impl;

import com.bl.weilan.common.ApiResponseCode;
import com.bl.weilan.common.CommonServiceException;
import com.bl.weilan.common.utils.ListUtils;
import com.bl.weilan.common.utils.UUIDUtils;
import com.bl.weilan.dao.model.StarInfoEntity;
import com.bl.weilan.dao.model.TeacherTrainEntity;
import com.bl.weilan.dao.model.TrainEntity;
import com.bl.weilan.dao.redis.model.UserInfo;
import com.bl.weilan.dao.repository.StarInfoRepository;
import com.bl.weilan.dao.repository.TeacherTrainRepository;
import com.bl.weilan.dao.repository.TrainRepository;
import com.bl.weilan.service.biz.train.TrainService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.train.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Log4j2
public class TrainServiceImpl implements TrainService {
    @Autowired
    TrainRepository trainRepository;

    @Autowired
    StarInfoRepository starInfoRepository;

    @Autowired
    TeacherTrainRepository teacherTrainRepository;

    @Override
    public TrainEntity getTrainById(String trainId) {
        TrainEntity trainEntity = trainRepository.findOne(trainId);
        return trainEntity;
    }

    @Override
    public TrainListVo getTrainLists(Pageable pageable, UserInfo userInfo){
        Page<TrainEntity> trainEntities;
        TrainListVo trainListVo = new TrainListVo();
        List<TrainVo> trainVos = new ArrayList<>();

        trainEntities = trainRepository.findByOrderByWeightDesc(pageable);
        for (TrainEntity trainEntity : trainEntities) {
            TrainVo trainVo = new TrainVo();
            BeanUtils.copyProperties(trainEntity, trainVo);
            if (userInfo != null) {
                StarInfoEntity starInfoEntity = new StarInfoEntity();
                starInfoEntity = starInfoRepository.findByTrainIdAndUserId(trainEntity.getId(), userInfo.getId());
                if(starInfoEntity == null) {
                    trainVo.setStar(false);
                } else {
                    trainVo.setStar(true);
                }
            } else {
                trainVo.setStar(false);
            }
            trainVos.add(trainVo);
        }


        trainListVo.setTotalCount(trainEntities.getTotalElements());
        trainListVo.setList(trainVos);

        return trainListVo;
    }

    @Override
    public void addTrain(TrainAddVo trainAddVo) {
        TrainEntity trainEntity = new TrainEntity();
        trainEntity.setId(UUIDUtils.getUUID());
        trainEntity.setCreateTime(new Date());
        trainEntity.setUpdateTime(new Date());
        BeanUtils.copyProperties(trainAddVo, trainEntity);
        trainRepository.save(trainEntity);

    }

    @Override
    public void del(DelBody id) {
        trainRepository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            trainRepository.delete(id);
        }
    }

    @Override
    public void modify(TrainModVo trainModVo){
        TrainEntity trainEntity = trainRepository.findOne(trainModVo.getId());

        if (trainEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应信息");
        }
        trainEntity.setUpdateTime(new Date());
        BeanUtils.copyProperties(trainModVo, trainEntity);
        trainRepository.save(trainEntity);
    }

    @Override
    public void addTeacherTrain(TeacherTrainAddVo teacherTrainAddVo) {
        TeacherTrainEntity teacherTrainEntity = new TeacherTrainEntity();
        teacherTrainEntity.setId(UUIDUtils.getUUID());
        teacherTrainEntity.setCreateTime(new Date());
        teacherTrainEntity.setUpdateTime(new Date());
        BeanUtils.copyProperties(teacherTrainAddVo, teacherTrainEntity);
        teacherTrainRepository.save(teacherTrainEntity);
    }

    @Override
    public void modTeacherTrain(TeacherTrainModVo teacherTrainModVo) {
        TeacherTrainEntity teacherTrainEntity = teacherTrainRepository.findOne(teacherTrainModVo.getId());
        teacherTrainEntity.setUpdateTime(new Date());
        BeanUtils.copyProperties(teacherTrainModVo, teacherTrainEntity);
        teacherTrainRepository.save(teacherTrainEntity);
    }

    @Override
    public TeacherTrainEntity getTeacherTrain() {
        List<TeacherTrainEntity> teacherTrainEntityList = teacherTrainRepository.findAll();
        TeacherTrainEntity teacherTrainEntity = new TeacherTrainEntity();
        teacherTrainEntity = ListUtils.getFirst(teacherTrainEntityList);
        return teacherTrainEntity;
    }

    @Override
    public void delTeacherTrain() {
        teacherTrainRepository.deleteAll();
    }
}
